// 基地址
const BASE_URL = "https://api-hmugo-web.itheima.net/api/public/v1/";

function request({
  url,
  method = "GET",
  data = {},
  header = {},
  tip = "拼命加载中...",
  isPullDown = false,
}) {
  return new Promise((resolve, reject) => {
    // 非下拉刷新时执行
    if (!isPullDown) {
      uni.showLoading({
        title: tip,
        mask: true,
      });
    }
    const token = uni.getStorageSync("token");
    console.log(token);
    if (token) {
      header.Authorization = toekn;
    }

    uni.request({
      // 拼接地址
      url: `${BASE_URL}${url}`,
      method,
      data,
      header,
      success: (res) => {
        resolve(res.data);
      },
      fail: (err) => {
        reject(err);
      },
      complete() {
        if (!isPullDown) {
          uni.hideLoading();
        }
      },
    });
  });
}
// 挂载到全局
uni.$request = request;
